/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ipcc.webservice.ws;

import com.ipcc.other.error.ERRORS;
import com.ipcc.utility.entity.IPCC_CUSTOMER;
import com.ipcc.utility.model.IPCC_CUSTOMERModel;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

/**
 *
 * @author Administrator
 */
@WebService(serviceName = "CustomerWS")
public class CustomerWS {

    /**
     * This is a sample web service operation
     */
    @WebMethod(operationName = "createCustomer")
    public String createCustomer(@WebParam(name = "orgCustomerId") String orgCustomerId,
            @WebParam(name = "name") String nameC,
            @WebParam(name = "phoneNumber") String phoneNumber,
            @WebParam(name = "note") String note) {
        //TODO write your implementation code here:
        try {
            IPCC_CUSTOMER ic = new IPCC_CUSTOMER();
            if (nameC == null || nameC.equalsIgnoreCase("")) return ERRORS.MISSING_NAMEC;
            else ic.setName(nameC);        

            if (orgCustomerId == null || orgCustomerId.equalsIgnoreCase("")) return ERRORS.MISSING_ORG_CUSTOMER_ID;
                else {
                    if (IPCC_CUSTOMERModel.getByOrg_customer_id(orgCustomerId) != null)
                        return ERRORS.ORG_CUSTOMER_ID_EXIST;
                    else ic.setOrg_customer_id(orgCustomerId);
                }

            if (phoneNumber == null || phoneNumber.equalsIgnoreCase("")) ic.setPhone_number("");
            else ic.setPhone_number(phoneNumber);

            if (note == null || note.equalsIgnoreCase("")) ic.setNote("");
            else ic.setNote(note);

            //INSERT CUSTOMER
            if (IPCC_CUSTOMERModel.insert(ic)) return ERRORS.SUCCESS;
            return ERRORS.FAIL;
            
        } catch (Exception e) {
            e.printStackTrace();
            return ERRORS.ERROR_INTERNAL;
        }
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "editCustomer")
    public String editCustomer(@WebParam(name = "orgCustomerId") String orgCustomerId,
            @WebParam(name = "nameC") String nameC,
            @WebParam(name = "phoneNumber") String phoneNumber,
            @WebParam(name = "note") String note) {
        try {
            IPCC_CUSTOMER ic = new IPCC_CUSTOMER();
            if (nameC == null || nameC.equalsIgnoreCase("")) return ERRORS.MISSING_NAMEC;
            else ic.setName(nameC);

            if (orgCustomerId == null || orgCustomerId.equalsIgnoreCase("")) return ERRORS.MISSING_ORG_CUSTOMER_ID;
            else {
                if (IPCC_CUSTOMERModel.getByOrg_customer_id(orgCustomerId) == null)
                    return ERRORS.ORG_CUSTOMER_ID_NOT_CORRECT;
                else ic.setOrg_customer_id(orgCustomerId);
            }

            if (phoneNumber == null || phoneNumber.equalsIgnoreCase("")) ic.setPhone_number("");
            else ic.setPhone_number(phoneNumber);

            if (note == null || note.equalsIgnoreCase(""))
                ic.setNote("");
            else ic.setNote(note);

            //EDIT CUSTOMER
            if (IPCC_CUSTOMERModel.update(ic)) return ERRORS.SUCCESS;
            return ERRORS.FAIL;
            
        } catch (Exception e) {
            e.printStackTrace();
            return ERRORS.ERROR_INTERNAL;
        }
    }
}
